<?php

namespace app\index\controller;

use app\admin\model\Calculation;
use app\admin\model\CalculationCurrency;
use app\admin\model\Chiap;
use app\admin\model\MallCate;
use app\admin\model\MallCurrency;
use app\admin\model\MallGoods;
use app\admin\model\MallGoodsNorms;
use app\admin\model\SystemBanner;
use app\admin\model\SystemNotice;
use app\common\controller\IndexController;
use app\Request;
use ClassLibrary\ClFile;
use ClassLibrary\ClHttp;
use EasyAdmin\upload\Uploadfile;
use think\App;
use think\facade\Env;

class Index extends IndexController {
    public function banner(Request $request) {
        $all      = input();
        $validate = \think\facade\Validate::rule([
            'type' => 'require',
        ]);
        if (!$validate->check($all)) {
            $this->error(0, $validate->getError());
        }
        $where = [
            'type'   => $all['type'],
            'status' => 1,
        ];
        $data  = SystemBanner::where($where)->select()->toArray();
        $this->success('查询成功', $data);
    }

    /**
     * 计算器页面
     * @return \think\response\View
     * User : Orange Tel: 181-3414-1353 QQ:1139552211
     * Date    : 2021/6/16 上午11:29
     */
    public function calculation() {

        $list = MallCate::instance()->where(['status' => 1])->select();
        return view('index/calculation', ['list' => $list]);
    }

    //推荐矿机
    public function recommend_goods() {
        $where = [
            'status'       => 1,
            'is_recommend' => 1,
        ];
        $data  = MallGoods::where($where)->field('id,virtual_sales,stock,sales,discount_price,title,produce_type,produce,type,datys')->select()->toArray();
        foreach ($data as $k => $v) {
            $data[$k]['produce_type'] = config('cointype')[$v['produce_type']];
            if ($v['type'] == 1) {
                $data[$k]['datys'] = '次日0:00';
            } else {
                $data[$k]['datys'] = $v['datys'] . '日0:00';
            }
        }
        $this->success('查询成功', $data);
    }

    //矿机分类
    public function mall_cate() {
        $data = MallCate::where('status', 1)->select()->toArray();
        $this->success('查询成功', $data);
    }

    //矿机列表
    public function goods_list() {
        $all      = input();
        $validate = \think\facade\Validate::rule([
            'page'    => 'require',
            'cate_id' => 'require',
        ]);
        if (!$validate->check($all)) {
            $this->error(0, $validate->getError());
        }
        $page  = isset($all['page']) && !empty($all['page']) ? $all['page'] : 1;
        $limit = isset($all['limit']) && !empty($all['limit']) ? $all['limit'] : 15;
        $where = [
            'status'  => 1,
            'cate_id' => $all['cate_id'],
        ];
        $data  = MallGoods::where($where)->field('id,virtual_sales,stock,sales,discount_price,title,produce_type,produce,type,datys')->page($page, $limit)->select()->toArray();
        foreach ($data as $k => $v) {
            $data[$k]['produce_type'] = config('cointype')[$v['produce_type']];
            if ($v['type'] == 1) {
                $data[$k]['datys'] = '次日0:00';
            } else {
                $data[$k]['datys'] = $v['datys'] . '日0:00';
            }
        }
        $this->success('查询成功', $data);
    }

    //矿机详情
    public function goods_info() {
        $all      = input();
        $validate = \think\facade\Validate::rule([
            'id' => 'require',
        ]);
        if (!$validate->check($all)) {
            $this->error(0, $validate->getError());
        }
        $where             = [
            'id' => $all['id'],
        ];
        $data              = MallGoods::where($where)->find();
        $currency_title    = MallCurrency::where('id', $data->currency_id)->find();
        $data->currency_id = $currency_title->title;
        $data->describe    = htmlspecialchars_decode($data->describe);
        $this->success('查询成功', $data);
    }

    public function goods_norms() {
        $all      = input();
        $validate = \think\facade\Validate::rule([
            'id' => 'require',
        ]);
        if (!$validate->check($all)) {
            $this->error(0, $validate->getError());
        }
        $where = [
            'goods_id' => $all['id'],
        ];
        $data  = MallGoodsNorms::where($where)->select()->toArray();
        foreach ($data as $k => $v) {
            if ($v['norms_stock'] <= 0) {
                $data[$k]['status'] = 0;
            } else {
                $data[$k]['status'] = 1;
            }
        }
        $this->success('OK', $data);
    }

    //云算力 推荐
    public function recommend_calculation() {
        $where = [
            'status'       => 1,
            'is_recommend' => 1,
        ];
        $model = new Calculation();
        $data  = $model->where($where)->alias('c')->field('c.id,c.stock,c.produce,c.payment_price,c.type,c.title,com.company as calculation_company_text,cur.title as calculation_currency_text,cur.company as calculation_currency_company,days.days as calculation_days_text')->leftJoin('calculation_company com', 'com.id=c.calculation_company_id')->leftJoin('calculation_currency cur', 'cur.id=c.calculation_currency_id')->leftJoin('calculation_days days', 'days.id=c.calculation_days_id')->select()->toArray();
        foreach ($data as $k => $v) {
            $res                = $model->add_user_coin_address();
            $data[$k]['amount'] = ($res['data']['usdt']['cny'] ?? 1) * $v['payment_price'];
            if ($v['type'] == 1) {
                $type = "现货";
            } else {
                $type = '期货';
            }
            $data[$k]['type_text'] = $type;
            $usdt                  = ClHttp::request('https://www.okex.com/api/spot/v3/instruments/' . $v['calculation_currency_company'] . '-USDT/ticker', '', 'json');
            $data[$k]['estimate']  = $v['produce'] * $v['calculation_days_text'] * $usdt['open_24h'];
        }

        //        foreach ($data as $k=>$v){
        ////            类型{radio}(1:现货,2:期货)
        //            if($v['type'] == 1){
        //                $type_text = '现货';
        //                $day = '次日交割';
        //            }else{
        //                $type_text = '期货';
        //                $day = $v['datys'].'天交割';
        //            }
        //            $data[$k]['type_text'] = $type_text;
        //            $data[$k]['combination'] = $v['calculation_currency_company'].$type_text.'云算力'.$v['calculation_days_text'].'天合约--'.$day;
        //            $data[$k]['number'] = $v['payment_price'].'  '.\think\facade\Config::get('cointype')[$v['payment_id']].'/'.$v['calculation_company_text'].'/天';
        //        }
        $this->success('查询成功', $data);
    }

    //云算力分类
    public function calculation_currency() {
        $data = CalculationCurrency::select()->toArray();
        $this->success('查询成功', $data);
    }

    //云算力 列表
    public function calculation_list() {
        $all      = input();
        $validate = \think\facade\Validate::rule([
            'page'                    => 'require',
            'calculation_currency_id' => 'require',
        ]);
        if (!$validate->check($all)) {
            $this->error(0, $validate->getError());
        }
        $page  = isset($all['page']) && !empty($all['page']) ? $all['page'] : 1;
        $limit = isset($all['limit']) && !empty($all['limit']) ? $all['limit'] : 15;
        $where = [
            'status'                    => 1,
            'c.calculation_currency_id' => $all['calculation_currency_id'],
        ];
        $model = new Calculation();
        $data  = $model->where($where)->alias('c')->field('c.*,com.company as calculation_company_text,cur.title as calculation_currency_text,cur.company as calculation_currency_company,days.days as calculation_days_text')->leftJoin('calculation_company com', 'com.id=c.calculation_company_id')->leftJoin('calculation_currency cur', 'cur.id=c.calculation_currency_id')->leftJoin('calculation_days days', 'days.id=c.calculation_days_id')->page($page, $limit)->order('id', 'desc')->select()->toArray();
        //        foreach ($data as $k=>$v){
        ////            类型{radio}(1:现货,2:期货)
        //            if($v['type'] == 1){
        //                $type_text = '现货';
        //                $day = '次日交割';
        //            }else{
        //                $type_text = '期货';
        //                $day = $v['datys'].'天交割';
        //            }
        //            $data[$k]['type_text'] = $type_text;
        //            $data[$k]['combination'] = $v['calculation_currency_company'].$type_text.'云算力'.$v['calculation_days_text'].'天合约--'.$day;
        //            $data[$k]['number'] = $v['payment_price'].'  '.\think\facade\Config::get('cointype')[$v['payment_id']].'/'.$v['calculation_company_text'].'/天';
        //        }
        foreach ($data as $k => $v) {
            $res                = $model->add_user_coin_address();
            $data[$k]['amount'] = ($res['data']['usdt']['cny'] ?? 1) * $v['payment_price'];
            if ($v['type'] == 1) {
                $type = "现货";
            } else {
                $type = '期货';
            }
            $data[$k]['type_text'] = $type;
            $usdt                  = ClHttp::request('https://www.okex.com/api/spot/v3/instruments/' . $v['calculation_currency_company'] . '-USDT/ticker', '', 'json');
            $data[$k]['estimate']  = $v['produce'] * $v['calculation_days_text'] * $usdt['open_24h'];
        }
        $this->success('查询成功', $data);
    }

    //云算力 详情
    public function calculation_info() {
        $all      = input();
        $validate = \think\facade\Validate::rule([
            'id' => 'require',
        ]);
        if (!$validate->check($all)) {
            $this->error(0, $validate->getError());
        }
        $where = [
            'c.id' => $all['id'],
        ];
        $data  = Calculation::where($where)->alias('c')->field('c.*,com.company as calculation_company_text,cur.title as calculation_currency_text,cur.company as calculation_currency_company,days.days as calculation_days_text')->leftJoin('calculation_company com', 'com.id=c.calculation_company_id')->leftJoin('calculation_currency cur', 'cur.id=c.calculation_currency_id')->leftJoin('calculation_days days', 'days.id=c.calculation_days_id')->find()->toArray();
        //            类型{radio}(1:现货,2:期货)
        if ($data['type'] == 1) {
            $type_text = '现货';
            $day       = '次日交割';
            $day_text  = '次日0:00交割';
        } else {
            $type_text = '期货';
            $day       = $data['datys'] . '天交割';
            $day_text  = $data['datys'] . '天0:00交割';
        }
        $data['type_text']   = $type_text;
        $data['day']         = $day_text;
        $data['combination'] = $data['calculation_currency_company'] . $type_text . '云算力' . $data['calculation_days_text'] . '天合约--' . $day;
        $data['number']      = $data['payment_price'] . '  ' . \think\facade\Config::get('cointype')[$data['payment_id']] . '/' . $data['calculation_company_text'] . '/天';
        $data['content']     = htmlspecialchars_decode($data['content']);
        $this->success('查询成功', $data);
    }

    public function coin_price() {
        $data     = [];
        $btc_usdt = ClHttp::request('https://www.okex.com/api/spot/v3/instruments/BTC-USDT/ticker', '', 'json');
        $etc_usdt = ClHttp::request('https://www.okex.com/api/spot/v3/instruments/ETH-USDT/ticker', '', 'json');
        $fil_usdt = ClHttp::request('https://www.okex.com/api/spot/v3/instruments/FIL-USDT/ticker', '', 'json');
        $xch_usdt = ClHttp::request('https://www.okex.com/api/spot/v3/instruments/XCH-USDT/ticker', '', 'json');

        //        24小时最高价
        if ($btc_usdt['last'] > $btc_usdt['open_24h']) {
            $btc_usdt_color = "#009900";
        } else {
            $btc_usdt_color = "#ee0000";
        }
        if ($etc_usdt['last'] > $etc_usdt['open_24h']) {
            $etc_usdt_color = "#009900";
        } else {
            $etc_usdt_color = "#ee0000";
        }
        if ($fil_usdt['last'] > $fil_usdt['open_24h']) {
            $fil_usdt_color = "#009900";
        } else {
            $fil_usdt_color = "#ee0000";
        }
        if ($xch_usdt['last'] > $xch_usdt['open_24h']) {
            $xch_usdt_color = "#009900";
        } else {
            $xch_usdt_color = "#ee0000";
        }
        array_push($data, ['name' => 'BTC/USDT', 'value' => $btc_usdt['last'], 'color' => $btc_usdt_color]);
        array_push($data, ['name' => 'ETH/USDT', 'value' => $etc_usdt['last'], 'color' => $etc_usdt_color]);
        array_push($data, ['name' => 'FIL/USDT', 'value' => $fil_usdt['last'], 'color' => $fil_usdt_color]);
        array_push($data, ['name' => 'XCH/USDT', 'value' => $xch_usdt['last'], 'color' => $xch_usdt_color]);
        $this->success('查询成功', $data);
    }

    public function notice() {
        $where = [
            'status' => 1,
            'type'   => 0
        ];
        $data  = SystemNotice::where($where)->select()->toArray();
        $this->success('查询成功', $data);
    }

    public function notice_info() {
        $all      = input();
        $validate = \think\facade\Validate::rule([
            'id' => 'require',
        ]);
        if (!$validate->check($all)) {
            $this->error(0, $validate->getError());
        }
        $where           = [
            'id' => $all['id'],
        ];
        $data            = SystemNotice::where($where)->find()->toArray();
        $data['content'] = htmlspecialchars_decode($data['content']);
        $this->success('查询成功', $data);
    }

    //-----------------chinaP盘-----------------------//
    public function chinap() {
        $page  = isset($all['page']) && !empty($all['page']) ? $all['page'] : 1;
        $limit = isset($all['limit']) && !empty($all['limit']) ? $all['limit'] : 15;
        $where = [
            'status' => 1,
        ];
        $model = new Chiap();
        $data  = $model->field('id,title,subtitle,image,sales,price')->where($where)->page($page, $limit)->order($this->sort)->select()->toArray();
        $this->success('查询成功', $data);
    }

    public function chinap_info() {
        $all      = input();
        $validate = \think\facade\Validate::rule([
            'id' => 'require',
        ]);
        if (!$validate->check($all)) {
            $this->error(0, $validate->getError());
        }
        $where           = [
            'c.id' => $all['id'],
        ];
        $model           = new Chiap();
        $data            = $model->alias('c')->field('c.id,c.title,c.subtitle,c.image,c.sales,c.price,c.stock,c.storage,c.chiap_company_id,c.type,c.days,c.content,chiapCompany.title as chiapCompanyTitle')->leftJoin('chiapCompany', 'chiapCompany.id = c.chiap_company_id')->where($where)->order($this->sort)->find()->toArray();
        $data['content'] = htmlspecialchars_decode($data['content']);
        $this->success('查询成功', $data);
    }

    public function avatarUpload(Request $request) {
        $data         = [
            'upload_type' => $request->post('upload_type'),
            'file'        => $request->file('file'),
        ];
        $uploadConfig = sysconfig('upload');
        empty($data['upload_type']) && $data['upload_type'] = $uploadConfig['upload_type'];
        $rule = [
            'upload_type|指定上传类型有误' => "in:{$uploadConfig['upload_allow_type']}",
            'file|文件'              => "require|file|fileExt:{$uploadConfig['upload_allow_ext']}|fileSize:{$uploadConfig['upload_allow_size']}",
        ];
        $this->validate($data, $rule);
        try {
            $upload = Uploadfile::instance()->setUploadType($data['upload_type'])->setUploadConfig($uploadConfig)->setFile($data['file'])->save();
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        if ($upload['save'] == true) {
            $this->success($upload['msg'], ['url' => $upload['url']]);
        } else {
            $this->error($upload['msg']);
        }
    }


}